package com.kuang.gulimall.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kuang.common.utils.PageUtils;
import com.kuang.common.utils.Query;
import com.kuang.common.vo.MemberRespVo;
import com.kuang.gulimall.member.dao.MemberDao;
import com.kuang.gulimall.member.dao.MemberLevelDao;
import com.kuang.gulimall.member.entity.MemberEntity;
import com.kuang.gulimall.member.entity.MemberLevelEntity;
import com.kuang.gulimall.member.execption.PhoneExistException;
import com.kuang.gulimall.member.execption.UsernameExistException;
import com.kuang.gulimall.member.service.MemberService;
import com.kuang.gulimall.member.vo.MemberRegistVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    MemberLevelDao memberLevelDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void regist(MemberRegistVo vo) {
        MemberDao memberDao = this.baseMapper;
        MemberEntity entity = new MemberEntity();

        //设置默认等级
        MemberLevelEntity levelEntity = memberLevelDao.getDefaultLevel();
        entity.setLevelId(levelEntity.getId());

        //保证唯一性，因为需要登录使用，必须唯一
        //为了让远程调用过来的上层controller能感知异常，可以使用异常机制
        checkPhoneUnique(vo.getPhone());
        entity.setMobile(vo.getPhone());
        checkUsernameUnique(vo.getUserName());
        entity.setUsername(vo.getUserName());

        entity.setNickname(vo.getUserName());

        //密码要进行加密存储，数据库明文存储就炸了(前后端交互是否也要加密)
        //进行盐值加密，避免数据被彩虹破解,加盐：
        //这里用springsecurity封装的盐值加密的方法来进行处理，数据库就存加密过的结果即可
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        entity.setPassword(bCryptPasswordEncoder.encode(vo.getPassword()));

        //其他的默认信息

        //保存
        memberDao.insert(entity);
    }

    //通过异常检验手机跟用户名是否在数据库中存在，如果存在就报异常
    @Override
    public void checkPhoneUnique(String phone) throws PhoneExistException{
        MemberDao baseMapper = this.baseMapper;
        Integer count = baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if (count>0){
            throw new PhoneExistException();
        }
    }

    @Override
    public void checkUsernameUnique(String userName) throws UsernameExistException{
        MemberDao baseMapper = this.baseMapper;
        Integer count = baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", userName));
        if (count>0){
            throw new PhoneExistException();
        }
    }

    @Override
    public MemberRespVo login(MemberRespVo vo) {
        MemberDao baseMapper = this.baseMapper;
        MemberEntity entity = baseMapper.selectOne(new QueryWrapper<MemberEntity>()
                .eq("username", vo.getUsername()).or().eq("mobile", vo.getMobile()));
        if (entity!=null){
            //登录成功，进行密码检测
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            boolean matches = bCryptPasswordEncoder.matches(vo.getPassword(), entity.getPassword());
            if (matches){
                MemberRespVo vo1 = new MemberRespVo();
                BeanUtils.copyProperties(entity,vo1);
                return vo1;
            }else return null;
        } else return null;
    }

}